const express = require('express');
const pool = require('../pool');
const r = express.Router();
   
// 所有项目查询
// http://127.0.0.1:8888/v1/product/all
r.get('/all',(req,res,next)=>{
  pool.query('select * from hqyc_item',(err,result)=>{
    if(err){
      next(err)
      return;
    }
    console.log(result);
    result.length>0?res.send({code:1,msg:'所有项目查询成功',data:result}):res.send({code:0,msg:'所有项目查询失败',data:result});
  })
})

// 项目类别查询
// http://127.0.0.1:8888/v1/product/allclass
r.get('/allclass',(req,res,next)=>{
  pool.query('select * from hqyc_class',(err,result)=>{
    if(err){
      next(err)
      return;
    }
    console.log(result);
    result.length>0?res.send({code:1,msg:'项目类别查询成功',data:result}):res.send({code:0,msg:'项目类别查询失败',data:result});
  })
})

//查询项目详情
// http://127.0.0.1:8888/v1/product/itemDetail
r.get('/itemDetail',(req,res,next)=>{
  let obj = req.query;
  // console.log(obj);
  pool.query('select * from hqyc_item_detail inner join hqyc_item on hqyc_item.tid=hqyc_item_detail.item_id where hqyc_item.item_name=?',[obj.tname],(err,result)=>{
    if(err){
      next(err);
      return;
    }
    if(result.length==0){
      res.send({
        code:201,
        msg:'数据未找到'
      });
    }else{
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }
  })
})

//查询必玩详情
// http://127.0.0.1:8888/v1/product/playDetail
r.get('/playDetail',(req,res,next)=>{
  let obj = req.query;
  // console.log(obj);
  pool.query('select * from hqyc_play where topicname=?',[obj.name],(err,result)=>{
    if(err){
      next(err)
      return
    }
    // console.log(result);
    result.length>0?res.send({code:1,msg:'数据查询成功',data:result}):res.send({code:0,msg:'数据查询失败',data:result});
  })
})

module.exports = r;